package cn.itcast.controller.system;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.system.Dept;
import cn.itcast.domain.system.Role;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.DeptService;
import cn.itcast.service.system.RoleService;
import cn.itcast.service.system.UserService;
import cn.itcast.utils.MailUtil;
import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private DeptService deptService;

    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/list",name = "查询所有用户信息")
    public String findAll(String companyId, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<User> pageInfo = userService.findByPage(page, pageSize, getCompanyId());
        request.setAttribute("page", pageInfo);
        return "/system/user/user-list";
    }

    @RequestMapping(value = "toAdd",name = "跳转到新增用户页面")
    public String toAdd() {
        List<Dept> deptList = deptService.findAll(getCompanyId());
        request.setAttribute("deptList", deptList);
        return "/system/user/user-add";
    }

    @RequestMapping(value = "/toUpdate",name = "跳转到修改用户页面")
    public String toUpdate(String id) {
        User user = userService.findById(id);
        request.setAttribute("user", user);
        List<Dept> deptList = deptService.findAll(getCompanyId());
        request.setAttribute("deptList", deptList);
        return "/system/user/user-update";
    }

    @RequestMapping(value = "edit",name = "保存用户信息")
    public String save(User user) throws Exception {
        User OUser = userService.findById(user.getId());
        // 判断数据库中是否有该用户信息，并且邮箱和密码是否修改了（在原加密之后的密码上增删两端字符不算修改），无法解决密码没有修改但邮箱修改的情况
        if (OUser != null && OUser.getPassword().contains(user.getPassword()) && OUser.getEmail().equals(user.getEmail())) {
            user.setPassword(OUser.getPassword());
        }

        user.setCompanyId(getCompanyId());
        user.setCompanyName(getCompanyName());
        if (StringUtils.isEmpty(user.getId())) {
            String id = UUID.randomUUID().toString();
            user.setId(id);
            userService.save(user);
        } else {
            userService.updateById(user);
        }
        return "redirect:/system/user/list.do";
    }

    @RequestMapping(value = "delete",name = "删除用户信息")
    public String deleteById(String id) {
        userService.deleteRoleAndUserByUserId(id);// 由于外键约束，因此先删除此用户所有的中间表数据
        userService.deleteById(id);
        return "redirect:/system/user/list.do";
    }

    @RequestMapping(value = "roleList", name = "跳转到用户的角色页面")
    public String roleList(String id) {
        List<Role> roleList = roleService.findAll(getCompanyId());
        request.setAttribute("roleList", roleList);
        User user = userService.findById(id);
        request.setAttribute("user", user);
        String userRoleStr = roleService.findRoleByUserid(id);
        request.setAttribute("userRoleStr", userRoleStr);
        return "system/user/user-role";
    }

    @RequestMapping(value = "changeRole", name = "更改用户的角色信息")
    public String changeRole(String userid, String[] roleIds) {
        userService.saveUserAndRole(userid, roleIds);
        User user = userService.findById(userid);
        request.setAttribute("user", user);
        String userRoleStr = roleService.findRoleByUserid(userid);
        request.setAttribute("userRoleStr", userRoleStr);
        List<Role> roleList = roleService.findAll(getCompanyId());
        request.setAttribute("roleList", roleList);
        return "redirect:/system/user/list.do";
    }
}
